var app = getApp();
var util = require("../../utils/libs.js")
var api = require("../../utils/api.js")
Page({
  data: {
    image: '',          
    title: '',
    content:"",
    uid:"",
    imgUrl:"",
    id:'',
    time:0,
    is_draft:1,
    textNum:0,
    time:0,
    step:0,
   first:0,
    shi:0,
    last:0,
    second:0,
    seconds:0,
    fen:0,
  },
  onLoad (options) {
    let that = this
    var id=wx.getStorageSync("uid")
    that.setData({
      uid:id,
      id:options.id,
      step:options.step,
      time:options.time,
      text_num:options.text_num
    })
  },
  getTime(time,types){
    let that = this
    if(types==10||types==30){
      var minutes = Math.floor(time / 60) + ""
      if (minutes >= 10) {
        minutes = minutes.split("")
        that.setData({
          first: 0,
          shi: 0,
          last:minutes[0],
          fen:minutes[1]
        })
      } else {
        that.setData({
          last: 0,
          fen: minutes
        })
      }
      var sendtis = Math.floor(time % 60) + ""
      if (sendtis >= 10) {
        var arr = sendtis.split("")
        that.setData({
          second: arr[0],
          seconds: arr[1]
        })
      } else {
        that.setData({
          second: 0,
          seconds: sendtis
        })
      }
    }else if(types==3||types==1){
        var house = Math.floor(time/60/60) + '';
        console.log(house)
        if (house >= 10) {
          that.setData({
            first:house[0],
            shi: house[1]
          })
        } else {
          that.setData({
            first: 0,
            shi: house
          })
        }
        var minutes = Math.floor((time/60 )% 60) + ""
        console.log(minutes, minutes[0], minutes[1])
        if (minutes >= 10) {
          minutes = minutes.split("")
          that.setData({
            last: minutes[0],
            fen: minutes[1]
          })
        } else {
          that.setData({
            last: 0,
            fen: minutes
          })
        }
        var sendtis = Math.floor(time % 60) + ""
        if (sendtis >= 10) {
          var arr = sendtis.split("")
          that.setData({
            second: arr[0],
            seconds: arr[1]
          })
        } else {
          that.setData({
            second: 0,
            seconds: sendtis
          })
        } 
    }
  },
   closeImages(){
    this.setData({
      imgUrl:''
    })
  },
  chooseImageTap (){
    let _this = this;
    wx.showActionSheet({
     itemList: ['从相册中选择', '拍照'],
     itemColor: "#f7982a",
     success: function(res) {
     if (!res.cancel) {
     if(res.tapIndex == 0){
     _this.chooseWxImage('album')
     }else if(res.tapIndex == 1){
     _this.chooseWxImage('camera')
     }
     }
     },
     fail: function(res) {
      console.log(res.errMsg)
    }
    })
},
chooseWxImage:function(type){
  let _this = this;
  wx.chooseImage({
   sizeType: ['original', 'compressed'],
   sourceType: [type],
   success: function (res) {
   _this.setData({
   logo: res.tempFilePaths[0],
   })
   app.upload_file("https://bucuo.jikeyun.net/api/upload",
    _this.data.logo,"upload",{},function success(res){
        res = JSON.parse(res)
      if(res.error_code === 0){
        _this.setData({
          imgUrl:res.data.file
        })
      }
    })
 }
})
},
bindTextAreaBlur(e){
    let content=e.detail.value
    let that = this
    that.setData({
      content:content,
      textNum:content.length
    })
  },
  addmend(){
    let that = this
    console.log(that.data.times)
    if(that.data.times==0){
      wx.showToast({
        title: '已经超时，请重新编辑',
        icon:'none',
        duration:1000
      })
      return ;
    }
    if(that.data.content==''){
      wx.showToast({
        title: '请输入文章内容',
        icon:'none',
        duration:1000
      })
      return false;
    }
    util.request(api.addBuildArticle,{
      uid:that.data.uid,
      content:that.data.content,
      image:that.data.imgUrl,
      step:that.data.step,
      id:that.data.id
    },"POST")
    .then(function (res) {
      if(res.error_code === 0){
        wx.showToast({
          title: '发布成功',
          icon:'success',
          duration:1000
        })
        setTimeout(function(){
          let id = res.data.id
          wx.navigateTo({
            url: '/pages/dragonarticle/dragonarticle?id=' + id
          })
        },1500)
      }
    })
  },
  onReady () {
    // 页面渲染完成
     let that = this
    if(this.data.time==30){
      var time = 30*60;
      var timer = setInterval(function(){
        time--;
        that.getTime(time,30)
      },1000)
      if(time==0){
        clearInterval(timer)
      }
    }else if(that.data.time==10){
      var time = 10 * 60;
      var timer = setInterval(function () {
        time--;
        that.getTime(time, 10)
      },1000)
      if (time == 0) {
        clearInterval(timer)
      }
    }else if(this.data.time==1){
      var time = 24*60*60;
      var timer = setInterval(function(){
        time--;
        that.getTime(time,1)
      },1000)
      if(time==0){
        clearInterval(timer)
      }
    }else if(this.data.time==3){
      var time = 3*60*60
      var timer = setInterval(function(){
       time--;
       that.getTime(time,3)
      },1000)
    }
  },
  onShow () {
    // 页面显示
  },
  onHide () {
    // 页面隐藏

  },
  onUnload () {
    // 页面关闭

  }
})
